package com.example.demo.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.example.demo.dao.UserRoleRepository;
import com.example.demo.entity.UserRole;

@Service
public class UserRoleService {
    private final UserRoleRepository userRoleRepository;

    public UserRoleService(UserRoleRepository userRoleRepository) {
        this.userRoleRepository = userRoleRepository;
    }

    public UserRole getByName(String name) {
        return userRoleRepository.findByName(name);
    }

    public UserRole getById(Integer id) {
        // userRoleRepository.findById()方法返回值类型为Optional<UserRole>
        return userRoleRepository.findById(id).orElse(null);
    }

    public void save(UserRole userRole) {
        userRoleRepository.save(userRole);
    }

    public void deleteById(Integer id) {
        userRoleRepository.deleteById(id);
    }

    // public void update(UserRole userRole) {
    //     userRoleRepository.save(userRole);
    // }

    public List<UserRole> getAll() {
        return userRoleRepository.findAll();
    }
}
